package com.tianqu.tms.www.car.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;

public class CarLicensePlateValidateController extends TmsController{
	private Logger logger = Logger.getLogger(CarLicensePlateValidateController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			CarCreationInputViewHelper carCreationInputViewHelper = new CarCreationInputViewHelper();
			carCreationInputViewHelper = (CarCreationInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			if("0".equals(carCreationInputViewHelper.getPrice())){
				carCreationInputViewHelper.setPrice("");
			}
			if("0".equals(carCreationInputViewHelper.getMileage())){
				carCreationInputViewHelper.setMileage("");
			}
			if("0".equals(carCreationInputViewHelper.getDisplacement())){
				carCreationInputViewHelper.setDisplacement("");
			}
			
			Car car = Car.getInstance();
			if(carCreationInputViewHelper.getSaveFlag() == 9){
				car = car.findCarByLicensePlate(carCreationInputViewHelper.getLicensePlate(),1);
			}else if(carCreationInputViewHelper.getSaveFlag() == 10){
				car = car.findCarByLicensePlate(carCreationInputViewHelper.getTempLicensePlate(),2);
			}
			 
			if(!StringUtil.isNullOrBlank(car.getSerial())){
				carCreationInputViewHelper.setLicensePlate(car.getLicensePlate());
				carCreationInputViewHelper.setSerial(car.getSerial());
				carCreationInputViewHelper.setDisplacement(String.valueOf(car.getDisplacement()));
				carCreationInputViewHelper.setGearbox(String.valueOf(car.getGearbox()));
				carCreationInputViewHelper.setVendorId(car.getVendorId());
				carCreationInputViewHelper.setBrandId(car.getBrandId());
				carCreationInputViewHelper.setCarStyleId(car.getCarStyleId());
				carCreationInputViewHelper.setCarStyle(car.getCarStyleId());
				carCreationInputViewHelper.setRegDate(car.getRegDate());
				carCreationInputViewHelper.setSendCardDate(car.getSendCardDate());
				carCreationInputViewHelper.setImg1(car.getPhoto_1());
				carCreationInputViewHelper.setImg2(car.getPhoto_2());
				carCreationInputViewHelper.setImg3(car.getPhoto_3());
				carCreationInputViewHelper.setImg4(car.getPhoto_4());
				carCreationInputViewHelper.setImg5(car.getPhoto_5());
				carCreationInputViewHelper.setImg6(car.getPhoto_6());
				carCreationInputViewHelper.setImg7(car.getPhoto_7());
				carCreationInputViewHelper.setImg8(car.getPhoto_8());
				carCreationInputViewHelper.setAttestStatus(car.getAttestStatus());
				carCreationInputViewHelper.setCarOwner(car.getCarOwner());
				carCreationInputViewHelper.setTempLicensePlate(car.getTempLicensePlate());
				carCreationInputViewHelper.setColor(String.valueOf(car.getColor()));
				carCreationInputViewHelper.setJqx(car.getJqx());
				carCreationInputViewHelper.setYearCarefulPeriod(car.getYearCarefulPeriod());
				carCreationInputViewHelper.setCompanyContactPerson(car.getCompanyContactPerson());
				carCreationInputViewHelper.setCompanyContactPhone(car.getCompanyContactPhone());
				carCreationInputViewHelper.setRemark(car.getRemark());
				carCreationInputViewHelper.setVin(car.getVin());
				
				if(car.getPrice() == 0){
					carCreationInputViewHelper.setPrice("");
				}else{
					carCreationInputViewHelper.setPrice(String.valueOf(car.getPrice()));
				}
				if(car.getMileage() == 0){
					carCreationInputViewHelper.setMileage("");
				}else{
					carCreationInputViewHelper.setMileage(String.valueOf(car.getMileage()));
				}
							
				carCreationInputViewHelper.setMemberId(String.valueOf(car.getMemberId()));
				if(!StringUtil.isNullOrBlank(car.getSerial())){//取过户表中第一张照片
					//modify by huangwei
					carCreationInputViewHelper.setCalibrationOn(1);//显示打印校准证
					//
					TransferAcceptedInfo transferAcceptedInfo = TransferAcceptedInfo.getInstance();
					transferAcceptedInfo = transferAcceptedInfo.findTransferByBasicCarId(car.getSerial());
					if(transferAcceptedInfo != null){
						if(!StringUtil.isNullOrBlank(transferAcceptedInfo.getPhoto_1()) && 
								("null".equals(transferAcceptedInfo.getCy_printTime()) || "".equals(transferAcceptedInfo.getCy_printTime()))
								&& transferAcceptedInfo.getStatus() == 0){
							carCreationInputViewHelper.setTransferPhoto(transferAcceptedInfo.getPhoto_1());
							carCreationInputViewHelper.setCheckPrint("1");//显示打印查验表
						}else if(!StringUtil.isNullOrBlank(transferAcceptedInfo.getPhoto_1()) && 
								(!"null".equals(transferAcceptedInfo.getCy_printTime()) && !"".equals(transferAcceptedInfo.getCy_printTime()))
								&& transferAcceptedInfo.getStatus() == 0){
							carCreationInputViewHelper.setCheckPrint("2");//显示打印流程表
						}
					}
				}else{
					carCreationInputViewHelper.setCalibrationOn(2);//不显示打印校准证
				}
				
				uc.setAttribute(TmsConstants.VIEWHELP, carCreationInputViewHelper);
			}
			
			controller.commit();
			logger.debug(this.getClass().getName() + "#execute was completed.");
			
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		
	}

}
